#pragma once

#include "paket.h"
#include <math.h>

namespace serial {


	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{		
	public:		

		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	public: System::IO::Ports::SerialPort^  port;
	protected: 

	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  x_box;
	private: System::Windows::Forms::TextBox^  y_box;
	private: System::Windows::Forms::Timer^  ispis_timer;
	private: System::Windows::Forms::TextBox^  r2_box;
	private: System::Windows::Forms::TextBox^  r1_box;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  x_calc_box;

	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Label^  label7;
	private: System::Windows::Forms::TextBox^  r1_txt_corrected;
	private: System::Windows::Forms::TextBox^  r2_txt_corrected;
	private: System::Windows::Forms::Label^  label8;
	private: System::Windows::Forms::Label^  label9;
	private: System::Windows::Forms::TextBox^  r1_txt_corrected_2;
	private: System::Windows::Forms::TextBox^  r2_txt_corrected_2;
	private: System::Windows::Forms::Label^  label10;
	private: System::Windows::Forms::Label^  label11;
	private: System::Windows::Forms::TextBox^  x_txt_Misha;
	private: System::Windows::Forms::TextBox^  y_txt_Misha;
	private: System::Windows::Forms::Button^  btn_send_data;

	private: System::Windows::Forms::TextBox^  txt_send_to_port;
	private: System::Windows::Forms::TextBox^  txt_motor;
	private: System::Windows::Forms::TextBox^  txt_smer;
	private: System::Windows::Forms::Label^  label12;
	private: System::Windows::Forms::Label^  label13;
	private: System::Windows::Forms::Label^  label14;

	private: 

	private: System::Windows::Forms::Button^  btn_go;
	private: System::Windows::Forms::Label^  label15;
	private: System::Windows::Forms::TextBox^  txt_port_name;

	private: System::Windows::Forms::Button^  btn_speed_test;
	private: System::Windows::Forms::Button^  btn_stop;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::ListBox^  listBox1;
	private: System::Windows::Forms::Button^  btn_speed_test_fixed;
	private: System::Windows::Forms::TextBox^  txt_speed_test_value;


	protected: 
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->port = (gcnew System::IO::Ports::SerialPort(this->components));
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->x_box = (gcnew System::Windows::Forms::TextBox());
			this->y_box = (gcnew System::Windows::Forms::TextBox());
			this->ispis_timer = (gcnew System::Windows::Forms::Timer(this->components));
			this->r2_box = (gcnew System::Windows::Forms::TextBox());
			this->r1_box = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->x_calc_box = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->r1_txt_corrected = (gcnew System::Windows::Forms::TextBox());
			this->r2_txt_corrected = (gcnew System::Windows::Forms::TextBox());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->label9 = (gcnew System::Windows::Forms::Label());
			this->r1_txt_corrected_2 = (gcnew System::Windows::Forms::TextBox());
			this->r2_txt_corrected_2 = (gcnew System::Windows::Forms::TextBox());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->label11 = (gcnew System::Windows::Forms::Label());
			this->x_txt_Misha = (gcnew System::Windows::Forms::TextBox());
			this->y_txt_Misha = (gcnew System::Windows::Forms::TextBox());
			this->btn_send_data = (gcnew System::Windows::Forms::Button());
			this->txt_send_to_port = (gcnew System::Windows::Forms::TextBox());
			this->txt_motor = (gcnew System::Windows::Forms::TextBox());
			this->txt_smer = (gcnew System::Windows::Forms::TextBox());
			this->label12 = (gcnew System::Windows::Forms::Label());
			this->label13 = (gcnew System::Windows::Forms::Label());
			this->label14 = (gcnew System::Windows::Forms::Label());
			this->btn_go = (gcnew System::Windows::Forms::Button());
			this->label15 = (gcnew System::Windows::Forms::Label());
			this->txt_port_name = (gcnew System::Windows::Forms::TextBox());
			this->btn_speed_test = (gcnew System::Windows::Forms::Button());
			this->btn_stop = (gcnew System::Windows::Forms::Button());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->btn_speed_test_fixed = (gcnew System::Windows::Forms::Button());
			this->txt_speed_test_value = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// port
			// 
			this->port->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::port_DataReceived);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(161, 15);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(24, 24);
			this->label1->TabIndex = 0;
			this->label1->Text = L"X";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(161, 50);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(22, 24);
			this->label2->TabIndex = 1;
			this->label2->Text = L"Y";
			// 
			// x_box
			// 
			this->x_box->Location = System::Drawing::Point(191, 12);
			this->x_box->Name = L"x_box";
			this->x_box->Size = System::Drawing::Size(100, 29);
			this->x_box->TabIndex = 2;
			// 
			// y_box
			// 
			this->y_box->Location = System::Drawing::Point(191, 47);
			this->y_box->Name = L"y_box";
			this->y_box->Size = System::Drawing::Size(100, 29);
			this->y_box->TabIndex = 3;
			// 
			// ispis_timer
			// 
			this->ispis_timer->Enabled = true;
			this->ispis_timer->Tick += gcnew System::EventHandler(this, &Form1::ispis_timer_Tick);
			// 
			// r2_box
			// 
			this->r2_box->Location = System::Drawing::Point(191, 117);
			this->r2_box->Name = L"r2_box";
			this->r2_box->Size = System::Drawing::Size(100, 29);
			this->r2_box->TabIndex = 7;
			// 
			// r1_box
			// 
			this->r1_box->Location = System::Drawing::Point(191, 82);
			this->r1_box->Name = L"r1_box";
			this->r1_box->Size = System::Drawing::Size(100, 29);
			this->r1_box->TabIndex = 6;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(150, 120);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(33, 24);
			this->label3->TabIndex = 5;
			this->label3->Text = L"R2";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(150, 85);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(33, 24);
			this->label4->TabIndex = 4;
			this->label4->Text = L"R1";
			// 
			// x_calc_box
			// 
			this->x_calc_box->Location = System::Drawing::Point(191, 368);
			this->x_calc_box->Name = L"x_calc_box";
			this->x_calc_box->Size = System::Drawing::Size(100, 29);
			this->x_calc_box->TabIndex = 9;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(62, 371);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(119, 24);
			this->label5->TabIndex = 8;
			this->label5->Text = L"X_calculated";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(45, 155);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(136, 24);
			this->label6->TabIndex = 4;
			this->label6->Text = L"R1_korigovano";
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Location = System::Drawing::Point(49, 190);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(136, 24);
			this->label7->TabIndex = 5;
			this->label7->Text = L"R2_korigovano";
			// 
			// r1_txt_corrected
			// 
			this->r1_txt_corrected->Location = System::Drawing::Point(191, 152);
			this->r1_txt_corrected->Name = L"r1_txt_corrected";
			this->r1_txt_corrected->Size = System::Drawing::Size(100, 29);
			this->r1_txt_corrected->TabIndex = 6;
			// 
			// r2_txt_corrected
			// 
			this->r2_txt_corrected->Location = System::Drawing::Point(191, 187);
			this->r2_txt_corrected->Name = L"r2_txt_corrected";
			this->r2_txt_corrected->Size = System::Drawing::Size(100, 29);
			this->r2_txt_corrected->TabIndex = 7;
			// 
			// label8
			// 
			this->label8->AutoSize = true;
			this->label8->Location = System::Drawing::Point(25, 225);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(156, 24);
			this->label8->TabIndex = 4;
			this->label8->Text = L"R1_korigovano_2";
			// 
			// label9
			// 
			this->label9->AutoSize = true;
			this->label9->Location = System::Drawing::Point(25, 260);
			this->label9->Name = L"label9";
			this->label9->Size = System::Drawing::Size(156, 24);
			this->label9->TabIndex = 5;
			this->label9->Text = L"R2_korigovano_2";
			// 
			// r1_txt_corrected_2
			// 
			this->r1_txt_corrected_2->Location = System::Drawing::Point(191, 222);
			this->r1_txt_corrected_2->Name = L"r1_txt_corrected_2";
			this->r1_txt_corrected_2->Size = System::Drawing::Size(100, 29);
			this->r1_txt_corrected_2->TabIndex = 6;
			// 
			// r2_txt_corrected_2
			// 
			this->r2_txt_corrected_2->Location = System::Drawing::Point(191, 257);
			this->r2_txt_corrected_2->Name = L"r2_txt_corrected_2";
			this->r2_txt_corrected_2->Size = System::Drawing::Size(100, 29);
			this->r2_txt_corrected_2->TabIndex = 7;
			// 
			// label10
			// 
			this->label10->AutoSize = true;
			this->label10->Location = System::Drawing::Point(97, 298);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(84, 24);
			this->label10->TabIndex = 4;
			this->label10->Text = L"X_Misha";
			// 
			// label11
			// 
			this->label11->AutoSize = true;
			this->label11->Location = System::Drawing::Point(97, 333);
			this->label11->Name = L"label11";
			this->label11->Size = System::Drawing::Size(82, 24);
			this->label11->TabIndex = 5;
			this->label11->Text = L"Y_Misha";
			// 
			// x_txt_Misha
			// 
			this->x_txt_Misha->Location = System::Drawing::Point(191, 293);
			this->x_txt_Misha->Name = L"x_txt_Misha";
			this->x_txt_Misha->Size = System::Drawing::Size(100, 29);
			this->x_txt_Misha->TabIndex = 6;
			// 
			// y_txt_Misha
			// 
			this->y_txt_Misha->Location = System::Drawing::Point(191, 328);
			this->y_txt_Misha->Name = L"y_txt_Misha";
			this->y_txt_Misha->Size = System::Drawing::Size(100, 29);
			this->y_txt_Misha->TabIndex = 7;
			// 
			// btn_send_data
			// 
			this->btn_send_data->Location = System::Drawing::Point(368, 368);
			this->btn_send_data->Name = L"btn_send_data";
			this->btn_send_data->Size = System::Drawing::Size(136, 45);
			this->btn_send_data->TabIndex = 10;
			this->btn_send_data->Text = L"Send Data";
			this->btn_send_data->UseVisualStyleBackColor = true;
			this->btn_send_data->Click += gcnew System::EventHandler(this, &Form1::btn_send_data_Click);
			// 
			// txt_send_to_port
			// 
			this->txt_send_to_port->Location = System::Drawing::Point(404, 295);
			this->txt_send_to_port->Name = L"txt_send_to_port";
			this->txt_send_to_port->Size = System::Drawing::Size(100, 29);
			this->txt_send_to_port->TabIndex = 7;
			// 
			// txt_motor
			// 
			this->txt_motor->Location = System::Drawing::Point(404, 260);
			this->txt_motor->Name = L"txt_motor";
			this->txt_motor->Size = System::Drawing::Size(100, 29);
			this->txt_motor->TabIndex = 7;
			// 
			// txt_smer
			// 
			this->txt_smer->Location = System::Drawing::Point(404, 330);
			this->txt_smer->Name = L"txt_smer";
			this->txt_smer->Size = System::Drawing::Size(100, 29);
			this->txt_smer->TabIndex = 7;
			// 
			// label12
			// 
			this->label12->AutoSize = true;
			this->label12->Location = System::Drawing::Point(314, 260);
			this->label12->Name = L"label12";
			this->label12->Size = System::Drawing::Size(58, 24);
			this->label12->TabIndex = 4;
			this->label12->Text = L"Motor";
			// 
			// label13
			// 
			this->label13->AutoSize = true;
			this->label13->Location = System::Drawing::Point(314, 295);
			this->label13->Name = L"label13";
			this->label13->Size = System::Drawing::Size(62, 24);
			this->label13->TabIndex = 5;
			this->label13->Text = L"Brzina";
			// 
			// label14
			// 
			this->label14->AutoSize = true;
			this->label14->Location = System::Drawing::Point(314, 333);
			this->label14->Name = L"label14";
			this->label14->Size = System::Drawing::Size(55, 24);
			this->label14->TabIndex = 8;
			this->label14->Text = L"Smer";
			// 
			// btn_go
			// 
			this->btn_go->Location = System::Drawing::Point(379, 12);
			this->btn_go->Name = L"btn_go";
			this->btn_go->Size = System::Drawing::Size(136, 45);
			this->btn_go->TabIndex = 10;
			this->btn_go->Text = L"Start";
			this->btn_go->UseVisualStyleBackColor = true;
			this->btn_go->Click += gcnew System::EventHandler(this, &Form1::btn_go_Click);
			// 
			// label15
			// 
			this->label15->AutoSize = true;
			this->label15->Location = System::Drawing::Point(349, 65);
			this->label15->Name = L"label15";
			this->label15->Size = System::Drawing::Size(43, 24);
			this->label15->TabIndex = 4;
			this->label15->Text = L"Port";
			// 
			// txt_port_name
			// 
			this->txt_port_name->Location = System::Drawing::Point(415, 63);
			this->txt_port_name->Name = L"txt_port_name";
			this->txt_port_name->Size = System::Drawing::Size(100, 29);
			this->txt_port_name->TabIndex = 6;
			this->txt_port_name->Text = L"COM1";
			// 
			// btn_speed_test
			// 
			this->btn_speed_test->Location = System::Drawing::Point(533, 12);
			this->btn_speed_test->Name = L"btn_speed_test";
			this->btn_speed_test->Size = System::Drawing::Size(136, 62);
			this->btn_speed_test->TabIndex = 10;
			this->btn_speed_test->Text = L"Speed Test Full";
			this->btn_speed_test->UseVisualStyleBackColor = true;
			this->btn_speed_test->Click += gcnew System::EventHandler(this, &Form1::btn_speed_test_Click);
			// 
			// btn_stop
			// 
			this->btn_stop->Location = System::Drawing::Point(379, 110);
			this->btn_stop->Name = L"btn_stop";
			this->btn_stop->Size = System::Drawing::Size(136, 45);
			this->btn_stop->TabIndex = 10;
			this->btn_stop->Text = L"STOP";
			this->btn_stop->UseVisualStyleBackColor = true;
			this->btn_stop->Click += gcnew System::EventHandler(this, &Form1::btn_stop_Click);
			// 
			// comboBox1
			// 
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(3) {L"b", L"l", L"d"});
			this->comboBox1->Location = System::Drawing::Point(533, 79);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(136, 32);
			this->comboBox1->TabIndex = 11;
			this->comboBox1->Text = L"b";
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->ItemHeight = 24;
			this->listBox1->Location = System::Drawing::Point(533, 254);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(120, 148);
			this->listBox1->TabIndex = 12;
			// 
			// btn_speed_test_fixed
			// 
			this->btn_speed_test_fixed->Location = System::Drawing::Point(533, 133);
			this->btn_speed_test_fixed->Name = L"btn_speed_test_fixed";
			this->btn_speed_test_fixed->Size = System::Drawing::Size(136, 69);
			this->btn_speed_test_fixed->TabIndex = 10;
			this->btn_speed_test_fixed->Text = L"Speed Test Fixed";
			this->btn_speed_test_fixed->UseVisualStyleBackColor = true;
			this->btn_speed_test_fixed->Click += gcnew System::EventHandler(this, &Form1::btn_speed_test_fixed_Click);
			// 
			// txt_speed_test_value
			// 
			this->txt_speed_test_value->Location = System::Drawing::Point(533, 208);
			this->txt_speed_test_value->Name = L"txt_speed_test_value";
			this->txt_speed_test_value->Size = System::Drawing::Size(135, 29);
			this->txt_speed_test_value->TabIndex = 7;
			this->txt_speed_test_value->Text = L"8";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(11, 24);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(691, 426);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->btn_speed_test_fixed);
			this->Controls->Add(this->btn_speed_test);
			this->Controls->Add(this->btn_stop);
			this->Controls->Add(this->btn_go);
			this->Controls->Add(this->btn_send_data);
			this->Controls->Add(this->x_calc_box);
			this->Controls->Add(this->label14);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->txt_smer);
			this->Controls->Add(this->txt_speed_test_value);
			this->Controls->Add(this->txt_motor);
			this->Controls->Add(this->txt_send_to_port);
			this->Controls->Add(this->y_txt_Misha);
			this->Controls->Add(this->r2_txt_corrected_2);
			this->Controls->Add(this->r2_txt_corrected);
			this->Controls->Add(this->x_txt_Misha);
			this->Controls->Add(this->r1_txt_corrected_2);
			this->Controls->Add(this->r1_txt_corrected);
			this->Controls->Add(this->label13);
			this->Controls->Add(this->label11);
			this->Controls->Add(this->label9);
			this->Controls->Add(this->r2_box);
			this->Controls->Add(this->label12);
			this->Controls->Add(this->label10);
			this->Controls->Add(this->label7);
			this->Controls->Add(this->label8);
			this->Controls->Add(this->txt_port_name);
			this->Controls->Add(this->r1_box);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label15);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->y_box);
			this->Controls->Add(this->x_box);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Margin = System::Windows::Forms::Padding(6, 5, 6, 5);
			this->Name = L"Form1";
			this->Text = L"Chupach";
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	public:
		Logika * core;
		volatile int _x, _y, _r1, _r2, _x_calc;
		double  _r1_korigovano_, _r2_korigovano_,_r1_korigovano_2,_r2_korigovano_2, x_Misha, y_Misha;
		unsigned char * primljeno;
		int element_number;

		int NAPRED;
		int NAZAD;

		int brzina_kretanja_levi;
		int brzina_kretanja_desni;
		int stoj;
		int trenutni_smer;
		int counter;
		int previous_x;

		System::Windows::Forms::ListBox^ lst;



		int packit(unsigned char H, unsigned char L)
		{
			int r = (((int)H)<<8) | ((int)L);
			if( H & 0x80 ) r |= 0xFFFF0000;
			return r;

		}
		
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
		{
			NAPRED = 1;
			NAZAD = -1;
			brzina_kretanja_levi= 8;
			brzina_kretanja_desni = 8;
			stoj = 0;
			trenutni_smer = NAPRED;
			counter = 0;
			previous_x = 0;
			this->lst = (gcnew System::Windows::Forms::ListBox());
			element_number = 0;
			core = new Logika();
			_x = _y = _r1 = _r2 = 0;
			primljeno = new unsigned char[30];
		}

	private: System::Void stop_engines(){
			if(!port->IsOpen)
			{
				port->PortName = txt_port_name->Text;
				port->Open();
			}
			walking('l',stoj,NAPRED);
			walking('d',stoj,NAPRED);
		}

	private: System::Void port_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) 
		{
			// Serial port ISR
			int size = port->BytesToRead;
			array<unsigned char> ^ data = gcnew array<unsigned char>(size);
			int r = port->Read(data, 0, size);

			for(int i=0; i<size; i++)
			{
				unsigned char bajt = data[i];
				if ( (element_number == 0) && (bajt == '@') )
				{
					element_number += 1;
				}
				else {
					if(element_number != 0) {
						primljeno[element_number-1] = bajt;
						element_number ++;
					}
				}

				if(element_number > 8)
				{
					element_number = 0;
					Paket p;
					double c = 2144;

					int x = packit(primljeno[0], primljeno[1]);					
					int y = packit(primljeno[2], primljeno[3]);
					int r1 = packit(primljeno[4], primljeno[5]),
						r2 = packit(primljeno[6], primljeno[7]);
					p.x = x;
					p.y = y;
					_x = x;
					_y = y;
					//_r1_korigovano_2 = r1 * 0.7;
					//_r2_korigovano_2 = r2 * 0.7;
					_r1_korigovano_2 = r1 * 3.2/2.9 - 281;
					_r2_korigovano_2 = r2 * 3.2/2.9 - 281;
					_r1_korigovano_ = (r1 * 343 * 3.2)/1000000.0;
					_r2_korigovano_ = (r2 * 343 * 3.2)/1000000.0;

					x_Misha = (Math::Pow(_r2_korigovano_2,2) + Math::Pow(c,2) - Math::Pow(_r1_korigovano_2,2))/(2*c);
					y_Misha = Math::Sqrt(Math::Pow(_r1_korigovano_2,2) - Math::Pow(x_Misha,2));

					///////////////////////////////////////////////////////////////////////////
					
					counter++;


					//if(counter == 5)
					{
						counter = 0;
						
						if(trenutni_smer == NAPRED)
						{
							if( x < (previous_x) )
							{
								if(x>250 && x<300)
								{
									walking('d',brzina_kretanja_desni,NAZAD);
									walking('l',brzina_kretanja_levi,NAZAD);								
								}
							}
							else
								this->lst->Items->Add(x);
						}
						else
						{
							if( x > (previous_x) )
							{
								if(x>1000 && x<1050)
								{
									walking('d',brzina_kretanja_desni,NAPRED);							
									walking('l',brzina_kretanja_levi,NAPRED);	
								}
							}
							else
								this->lst->Items->Add(x);
						}
					}

					///////////////////////////////////////////////////////////////////////////

					//_r1_korigovano_ = r1 * 343 * 3,2 * Math::Pow(10,-6);
					//_r2_korigovano_ = r2 * 343 * 3,2 * Math::Pow(10,-6);
					_r1 = r1;
					_r2 = r2;
					_x_calc = sqrt( ((double)r1 * r1) - (y * y) );
					previous_x = x;
					//core->add(p);
				}
			}
		}

	private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) 
		{
			// destruktor
			delete core;
			port->Close();
			core = NULL;
			delete [] primljeno;
			primljeno = NULL;
		}

	private: System::Void ispis_timer_Tick(System::Object^  sender, System::EventArgs^  e) 
		{
			try
			{
				x_box->Text = Convert::ToString( _x );
				y_box->Text = Convert::ToString( _y );
				r1_box->Text = Convert::ToString( _r1 );
				r2_box->Text = Convert::ToString( _r2 );
				x_calc_box->Text = Convert::ToString( _x_calc );
				r1_txt_corrected->Text = Convert::ToString( _r1_korigovano_ );
				r2_txt_corrected->Text = Convert::ToString( _r2_korigovano_ );
				r1_txt_corrected_2->Text = Convert::ToString( _r1_korigovano_2 );
				r2_txt_corrected_2->Text = Convert::ToString( _r2_korigovano_2 );
				x_txt_Misha->Text = Convert::ToString( y_Misha );
				y_txt_Misha->Text = Convert::ToString( x_Misha );
			}
			catch(...)
			{
				int i=0;
			}
		}

	private: int kretanje(char motor, int brzina, int smer)
		{
			trenutni_smer = smer;

			brzina *= smer;

			if(motor == 'l' || motor=='L')
			{
				if(brzina<0)
				{
					brzina=brzina*(-1);
					brzina = brzina + 128;
				}
				else
					brzina = brzina + 192;
			}
			else
			{
				if(motor == 'd' || motor == 'D')
				{
					if(brzina<0)
					{
						brzina=brzina*(-1);
						brzina = brzina + 64;
					}
				}
			}					
			return brzina;
		}

	private: System::Void walking(char motor, int brzina, int smer)
		 {
			 int brz = kretanje(motor,brzina,smer);
			 char* aa = (char*)&brz;
			 array<unsigned char>^ data = gcnew array<unsigned char>(1);
			 data[0] = aa[0];
			 port->Write(data,0,1);
		 }

	private: System::Void walking_2(char motor, int brzina, int smer)
		 {
			 int brz = brzina;
			 char* aa = (char*)&brz;
			 array<unsigned char>^ data = gcnew array<unsigned char>(1);
			 data[0] = aa[0];
			 //this->listBox1->Items->Add(data);
			 port->Write(data,0,1);
		 }


	private: System::Void btn_go_Click(System::Object^  sender, System::EventArgs^  e) {

			if(!port->IsOpen)
			{
				port->PortName = txt_port_name->Text;
				port->Open();
			}

			walking('l',brzina_kretanja_levi,NAPRED);
			walking('d',brzina_kretanja_desni,NAPRED);
		 }

private: System::Void btn_stop_Click(System::Object^  sender, System::EventArgs^  e) {
			 for(int i = 0 ; i < this->lst->Items->Count ; i++ )
				 this->listBox1->Items->Add(this->lst->Items[i]);
			stop_engines();
		 }

private: System::Void btn_send_data_Click(System::Object^  sender, System::EventArgs^  e) {
			 int brzina = kretanje(txt_motor->Text[0],Convert::ToInt32(txt_send_to_port->Text),Convert::ToInt32(txt_smer->Text));
			 char* aa = (char*)&brzina;
			 array<unsigned char>^ data = gcnew array<unsigned char>(1);
			 data[0] = aa[0];
			 port->Write(data,0,1);
		 }

private: System::Void btn_speed_test_Click(System::Object^  sender, System::EventArgs^  e) {
			 test_engine(this->comboBox1->Text[0],NAPRED);
		 }

private: System::Void test_engine(char which_engine, int smer){
			 if(!port->IsOpen)
			 {
				 port->PortName = txt_port_name->Text;
				 port->Open();
			 }
			 switch(which_engine)
			 {
			 case 'l':
				 {
					for(int j = 0 ; j < 64 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking_2('l',j+192*smer,trenutni_smer);
					 }
					for(int j = 64 ; j < 128 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking_2('l',j+64*smer,trenutni_smer);
					 }
					break;
				 }
			case 'd':
				{
					for(int j = 0 ; j < 63 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking_2('d',j*smer,trenutni_smer);
					 }
					for(int j = 64 ; j < 128 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking_2('d',j*smer,trenutni_smer);
					 }
					break;
				}
			 case 'b':
				 {
					 for(int j = 0 ; j < 63 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking_2('d',j*smer,trenutni_smer);
						 walking_2('l',j+192*smer,trenutni_smer);
					 }
					 for(int j = 64 ; j < 128 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking_2('d',j*smer,trenutni_smer);
						 walking_2('l',j+64*smer,trenutni_smer);
					 }
					break;
				 }
			 }

			stop_engines();
		 }

private: System::Void btn_speed_test_fixed_Click(System::Object^  sender, System::EventArgs^  e) {
			 test_engine_fixed(this->comboBox1->Text[0]);
		 }

private: System::Void test_engine_fixed(char which_engine){			 
			 if(!port->IsOpen)
			 {
				 port->PortName = txt_port_name->Text;
				 port->Open();
			 }
			 switch(which_engine)
			 {
			 case 'l':
				 {
					for(int j = 0 ; j < 63 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking('l',Convert::ToInt32(this->txt_speed_test_value->Text)*NAPRED,trenutni_smer);
					 }
					for(int j = 64 ; j < 128 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking('l',Convert::ToInt32(this->txt_speed_test_value->Text)*NAZAD,trenutni_smer);
					 }
					break;
				 }
			case 'd':
				{
					for(int j = 0 ; j < 63 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking('d',Convert::ToInt32(this->txt_speed_test_value->Text)*NAPRED,trenutni_smer);
					 }
					for(int j = 64 ; j < 128 ; j++)
					 {
						 Threading::Thread::Sleep(100);
						 walking('d',Convert::ToInt32(this->txt_speed_test_value->Text)*NAZAD,trenutni_smer);
					 }
					break;
				}
			 case 'b':
				 {
					 /*for(int j = 0 ; j < 100 ; j++)
					 {
						 Threading::Thread::Sleep(50);
						 walking('d',Convert::ToInt32(this->txt_speed_test_value->Text)*NAPRED,trenutni_smer);
						 walking('l',Convert::ToInt32(this->txt_speed_test_value->Text)*NAPRED,trenutni_smer);
					 }*/
					 
					 for(int j = 0 ; j < 100 ; j++)
					 {
						 Threading::Thread::Sleep(50);
						 walking('d',Convert::ToInt32(this->txt_speed_test_value->Text)*NAZAD,trenutni_smer);
						 walking('l',Convert::ToInt32(this->txt_speed_test_value->Text)*NAZAD,trenutni_smer);
					 }
					break;
				 }
			 }

			 stop_engines();
		 }

};
}


/*

	@
	xH
	xL
	yH
	yL
	r1H
	r1L
	r2H
	r2L

	funkcija x_rucno = sqrt(r1 ^ 2 - y ^ 2),
	r1,
	r2
*/
